<!DOCTYPE html>
<!--

// Copyright 2011 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

-->
<html>
<head>
<title></title>
<script src="../../../third_party/closure-library/closure/goog/base.js"></script>
<script>
goog.require('goog.testing.jsunit');
</script>
<script src="../../../src/traceur.js"></script>
</head>
<body>
<script type="text/traceur">

function testRestParameter() {
  function f(...p) {
    return p;
  }

  function g(a, ...p) {
    return p;
  }

  assertArrayEquals([], f());
  assertArrayEquals([0], f(0));
  assertArrayEquals([0, 1], f(0, 1));

  assertArrayEquals([], g());
  assertArrayEquals([], g(0));
  assertArrayEquals([1], g(0, 1));
  assertArrayEquals([1, 2], g(0, 1, 2));
}

</script>
<script>

// Replace the Closure-provided array comparer with our own that doesn't barf
// because Array.prototype has a __iterator__ method.
function assertArrayEquals(expected, actual) {
  assertEquals(JSON.stringify(expected, null, 2),
               JSON.stringify(actual, null, 2));
}

var TreeWriter = traceur.outputgeneration.TreeWriter;

function evalScript(scriptElement) {
  var source;
  var name;
  if (scriptElement.src) {
    name = scriptElement.src;
    var xhr = new XMLHttpRequest();
    // Synchronous load but these are already loaded when we get here so it
    // should be fast.
    xhr.open('GET', scriptElement.src, false);
    xhr.send();
    source = xhr.responseText;
  } else {
    name = 'inline-script';
    source = scriptElement.textContent;
  }

  var errorReporter = new traceur.util.ErrorReporter();
  var tree = transform(errorReporter, name, source);
  traceur.strictGlobalEval(TreeWriter.write(tree));
  assertFalse(errorReporter.hadError());
}

function evalAllScripts() {
  var scripts = document.querySelectorAll('script[type="text/traceur"]');
  Array.prototype.forEach.call(scripts, evalScript);
}

function transform(errorReporter, name, source) {
  var sourceFile = new traceur.syntax.SourceFile(name, source);
  var scanner = new traceur.syntax.Scanner(errorReporter, sourceFile);
  var parser = new traceur.syntax.Parser(errorReporter, scanner);
  var tree = parser.parseProgram();
  return traceur.codegeneration.RestParameterTransformer.transformTree(tree);
}

function testInvalidRestParameter() {
  var name = 'test';
  var source =
     'function invalidParam(noDefault, ...restParam, noRestAgain) {\n' +
     '  // Should fail to parse since non rest param is not allowed after\n' +
     '  // param.\n' +
     '}\n';

  var errors = [];

  var errorReporter = {
    reportError: function(position, message) {
      errors.push({position: String(position), message: message});
    }
  };

  transform(errorReporter, name, source);

  assertEquals('test(1, 46)', errors[0].position);
  assertEquals('\')\' expected', errors[0].message);
}

evalAllScripts();

</script>
</body>
</html>
